<?php


namespace app\wechat\controller;


use app\common\controller\WechatUserBase;
use app\common\model\Admin;
use app\common\model\ShopCash;
use app\common\model\ShopMoneyLog;
use think\Db;

class Store extends WechatUserBase
{
//    前置操作
    protected $beforeActionList = [
        'checkIsStoreUser'=>['except'=>'login,checkLogin'],
    ];
//    判断是不是已经绑定了商户id
    protected function checkIsStoreUser(){
        $user = session('wechat');
        if (empty($user['shop_id'])){
            $this->redirect('store/login');
        }
    }
    public function login(){
        $user = session('wechat');
        if (!empty($user['shop_id'])){
            $this->redirect('store/index');
        }
        return view();
    }
    //    商家登录检查
    public function checkLogin(){
        $param = input();
//        检查当前管理账号是不是存在
        $info = Admin::where('username',$param['username'])->find();
        if (!empty($info)){
            if ($info['state'] == 20){
                $this->error('这个账号已被禁用');
            }else{
                $userModel = new \app\common\model\User();
                $shopModel = new \app\common\model\Shop();
//                检查密码正确性
                if (isset($param['password']) && $param['password'] == encry_code($info['password'],'DECODE')){
                    $user_id = session('wechat.id');
                    $shop = Db::name('shop')->where('admin_id',$info['id'])->find();
//                    更新用户绑定店铺id
                    $userModel->isUpdate(true)->save(['shop_id'=>$shop['id']],['id'=>$user_id]);
                    if (empty($shop['user_id'])){
//                        如果该店铺对应的提现用户默认为空，那么就更新一个
                        $shopModel->isUpdate(true)->save(['user_id'=>$user_id],['id'=>$shop['id']]);
                    }
//                    更新session
                    session('wechat',Db::name('user')->find($user_id));
                    $this->success('登录成功');
                }else{
                    $this->error('密码错误');
                }
            }
        }else{
            $this->error('没有这个账号，检查账号是否正确');
        }
    }

    public function goods_index(){
        return view('store/goods/index');
    }
    public function goods_detail(){
        return view('store/goods/detail');
    }
    public function order_index(){
        return view('store/order/index');
    }
    public function order_detail(){
        return view('store/order/detail');
    }
    public function info_index(){
        return view('store/info/index');
    }
    public function info_info(){
        return view('store/info/info');
    }
    public function cash_index(){
        return view('store/cash/index');
    }
    public function cash_cash(){
        return view('store/cash/cash');
    }

    //    获取订单列表
    public function getOrderList(){
        $param = input();
        $where = [];


        if (is_numeric($param['order_status']) && $param['order_status'] > 0){
            $where[] = ['order_status','=',$param['order_status']];
        }

        if (isset($param['shop_id']) && is_numeric($param['shop_id']) && $param['shop_id'] > 0){
            $where[] = ['shop_id','=',$param['shop_id']];
        }

        $data = \app\common\model\Order::with(['goods','shop'])->where($where)->order('id desc')->paginate($param['limit'],false,['query'=>$param]);
        foreach ($data as $k=>&$v){
            if ($v['buy_mode'] == 'BUY'){
                $v['img_thumb'] = getFullPath($v['img_thumb']);
            }else{
                $v['img_thumb'] = getFullPath($v['shop_img_thumb']);
            }

            $v['pay_status_text'] = $v->pay_status_text;
            $v['order_status_text'] = $v->order_status_text;
            $v['buy_mode_text'] = $v->buy_mode_text;
            $v['total_price'] = number_format($v['total_price'],2)/100*100;
        }
        $data = $data->toArray();
        $this->result($data,$data['total']);
    }

    //    店铺详情
    public function getShopDetail(){
        $shop_id = input('shop_id');
        $data = \app\common\model\Shop::getDetail([['id','=',$shop_id]]);

        $this->result($data);
    }
    //    店铺编辑
    public function shopEdit(){
        $param = input();
        $shop = new \app\common\model\Shop();
        $shop->allowField(true)->save($param,['id'=>$param['id']]);
        $this->success("更改成功");
    }
    //    余额变更历史
    public function getMoneyLog(){
        $param = input();
        $where = [];
        if (!empty($param['shop_id'])){
            $where[] = ['shop_id','=',$param['shop_id']];
        }
        if (!empty($param['type']) && in_array($param['type'],['inc','dec'])){
            $where[] = ['type','=',$param['type']];
        }
        $data = ShopMoneyLog::where($where)->order('id desc')->paginate($param['limit']);
        $data = $data->toArray();
        $data['total_inc_money'] =Db::name('ShopMoneyLog')->where($where)->where('type','=','inc')->sum('value');
        $data['total_dec_money'] =Db::name('ShopMoneyLog')->where($where)->where('type','=','dec')->sum('value');
        $this->result($data);
    }
    //    提现申请
    public function cash(){
        $param = input();
        $shopCashModel = new ShopCash();
        $res = $shopCashModel->allowField(true)->isUpdate(false)->save($param);
        if ($res !== false){
            $this->success();
        }else{
            $this->error();
        }
    }

//    提现申请列表
    public function cashList(){
        $shop_id = input('shop_id');
        $data = ShopCash::where('shop_id',$shop_id)->order('id desc')->select();
        $this->result($data);
    }

}
